package com.louis.kitty.admin.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @author smilezmh
 */
public class FileTools {
    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean downloadFiles(String path, String fileName, HttpServletResponse response, HttpServletRequest request, boolean isPdfOnline) throws IOException {
        File scFile = new File(path);
        // 如果文件名不为空，则进行下载
        if (scFile != null) {
            // 如果文件名存在，则进行下载
            if (scFile.exists()) {
                // 配置文件下载
                response.reset();
                if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
//                    fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器
                    fileName = URLEncoder.encode(fileName, "UTF-8"); // firefox浏览器
                } else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
                    fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器
                } else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
                    fileName = URLEncoder.encode(fileName, "UTF-8"); // firefox浏览器
                }
                response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                //设置传输的类型
                if (isPdfOnline) {
                    URL u = new URL("file:///" + path);
                    response.addHeader("Content-Type", "image/jpeg ");
                    response.addHeader("Content-Type", "image/png");
                    response.setHeader("Content-Disposition", "inline;filename=" + fileName);
                } else {
                    response.setContentType("application/octet-stream;charset=utf-8");
                    response.setHeader("Content-Transfer-Encoding", "chunked");
                    response.setHeader("Access-Control-Allow-Origin", "*");
                    response.setHeader("Access-Control-Expose-Headers", "*");
                    response.setHeader("name", fileName);
                }
                // 实现文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;

                try {
                    fis = new FileInputStream(scFile);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }

                    os.flush();
                    return true;
                } catch (Exception e) {
                    P.throwMessageIf(true, "下载失败");
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        return false;
    }

    public static boolean downloadExcel(String path, String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException {
        File scFile = new File(path);
        // 如果文件名不为空，则进行下载
        if (scFile != null) {
            // 如果文件名存在，则进行下载
            if (scFile.exists()) {
                // 配置文件下载
                response.reset();
                if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
//                    fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器
                    fileName = URLEncoder.encode(fileName, "UTF-8"); // firefox浏览器
                } else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
                    fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器
                } else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
                    fileName = URLEncoder.encode(fileName, "UTF-8"); // firefox浏览器
                }
                response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                //设置传输的类型
                response.setHeader("Content-Transfer-Encoding", "chunked");
                response.setHeader("Access-Control-Expose-Headers", "*");
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=file.xlsx");
                response.setHeader("Access-Control-Allow-Origin", "*");
                // 实现文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;

                try {
                    fis = new FileInputStream(scFile);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }

                    os.flush();
                    return true;
                } catch (Exception e) {
                    P.throwMessageIf(true, "下载失败");
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        return false;
    }
}
